const { json } = require('express');
var express = require('express');
var router = express.Router();
//引入jwt工具类
var {createJwt} = require('../src/utils/jwtUtil')
//引入用户的业务层
var userService=require("../src/services/user/userService");

router.post("/login",(req,resp)=>{
   //获得用户名和密码

   console.log("接受到请求======>>>"+req.socket.remoteAddress);
   
   let username =req.body.username; 
   let pwd =req.body.pwd; 
   console.log(username,pwd);
   //调用业务层的登录方法
  let result = userService.login(username,pwd);
  result.then((data)=>{
     console.log(data);
        //登录成功
      if(data.length!=0){
          //创建jwt
         let token =  createJwt({"name":result.name});
         resp.end(JSON.stringify({...data,"_t":`Bearer ${token}`})); 
      }else{
         resp.end(JSON.stringify(data));
      }

  })
})


module.exports = router;